package com.patchmanage.back.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.patchmanage.back.dto.InventoryDto;
import com.patchmanage.back.entity.Inventory;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author huizhao
 * @since 2021-03-20
 */
public interface InventoryMapper extends BaseMapper<Inventory> {

    @Select({"<script>",
            "SELECT i.*,g.name group_name FROM inventory i",
            "left join department_group g on(i.group_id = g.id)",
            "WHERE 1=1 ",
            "<if test='map.id != null and map.id != &quot;&quot;'>",
            "AND i.id = #{map.id, jdbcType=INTEGER}",
            "</if>",
            "<if test='map.owner != null and map.owner != &quot;&quot;'>",
            "AND i.owner = #{map.owner}",
            "</if>",
            "<if test='map.os != null and map.os != &quot;&quot;'>",
            "AND i.os = #{map.os}",
            "</if>",
            "<if test='map.ip != null and map.ip != &quot;&quot;'>",
            "AND i.ip = #{map.ip}",
            "</if>",
            "<if test='map.safeLv!=null and map.safeLv != &quot;&quot;'>",
            "AND i.safe_lv = #{map.safeLv}",
            "</if>",
            "<if test='map.isTest!=null and map.isTest != &quot;&quot;'>",
            "AND i.is_test = #{map.isTest}",
            "</if>",
            "<if test='map.groupId != null'>",
            "AND i.group_id in ",
            "<foreach collection='map.groupId' item='item' open='(' separator=',' close=')'>",
            "#{item}",
             "</foreach>",
            "</if>",
            "<if test='map.isAvailable!=null and map.isAvailable != &quot;&quot;'>",
            "AND i.is_available = #{map.isAvailable}",
            "</if>",
            "<if test='map.ipSql != null'>",
            "AND ${map.ipSql}",
            "</if>",
            "<if test='map.search != null and map.search != &quot;&quot;'>",
            "AND (i.name like CONCAT('%',#{map.search},'%') or i.owner like CONCAT('%',#{map.search},'%') or i.ip like CONCAT('%',#{map.search},'%'))",
            "</if>",
            "order by i.safe_lv desc",
            "</script>"})
    IPage<InventoryDto> selectInventoryPage(IPage<InventoryDto> page, @Param("map") Map dict);

    @Select({"<script>",
            "SELECT * FROM inventory ",
            "WHERE 1=1 ",
            "<if test='map.id != null'>",
            "AND id = #{map.id, jdbcType=INTEGER}",
            "</if>",
            "<if test='map.owner != null'>",
            "AND owner = #{map.owner}",
            "</if>",
            "<if test='map.os != null'>",
            "AND os = #{map.os}",
            "</if>",
            "<if test='map.ip != null'>",
            "AND ip = #{map.ip}",
            "</if>",
            "<if test='map.safeLv!=null'>",
            "AND safe_lv = #{map.safeLv}",
            "</if>",
            "<if test='map.isTest!=null'>",
            "AND is_test = #{map.isTest}",
            "</if>",
            "<if test='map.groupId != null and map.groupId != &quot;&quot;'>",
            "AND group_id = #{map.groupId}",
            "</if>",
            "<if test='map.isAvailable!=null'>",
            "AND is_available = #{map.isAvailable}",
            "</if>",
            "<if test='map.ip != null'>",
            "AND #{map.ip}",
            "</if>",
            "<if test='map.ipSql != null'>",
            "AND ${map.ipSql}",
            "</if>",
            "<if test='map.search != null'>",
            "AND (name like CONCAT('%',#{map.search},'%') or owner like CONCAT('%',#{map.search},'%') or ip like CONCAT('%',#{map.search},'%'))",
            "</if>",
            "order by safe_lv desc",
            "</script>"})
    List<Inventory> selectInventory(@Param("map") Map dict);
}
